package com.site.dao.impl.backend.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.site.dao.backend.system.UsersDao;
import com.site.dto.backend.system.AuthorityBizBean;
import com.site.dto.backend.system.UsersBizBean;
import com.site.model.backend.system.UsersInfo;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.base.dao.HQLHelper;
import com.site.util.constant.Constants;
import com.site.util.exception.ApplicationException;
import com.site.util.extremetable.QueryPage;

@SuppressWarnings("all")
@Repository("usersDao")
public class UsersDaoImpl extends BaseDaoImpl implements UsersDao{

	@Override
	public UsersInfo getUserByName(String userName) {
		String hql = "from UsersInfo where userName = :userName and markDelete ='N' ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName);
		List<UsersInfo> objList = this.findByHQL(hql,params);
		if(objList.size()==0)
			return null;
		return (UsersInfo)objList.get(0);
	}

	@Override
	public List<UsersInfo> getUserList(UsersBizBean userBizBean, QueryPage queryPage) {
		HQLHelper hqlHelper = new HQLHelper("UsersInfo","user");
		hqlHelper.addEqual("markDelete", "N");
		if(null != userBizBean){
			if(!StringUtils.isEmpty(userBizBean.getUserName())){
				hqlHelper.addEqual("userName", userBizBean.getUserName());
			}
			if(!StringUtils.isEmpty(userBizBean.getRealName())){
				hqlHelper.addLike("realName", userBizBean.getRealName());
			}
		}
		
		String hql = hqlHelper.getHQL();
		List<UsersInfo> objList = new ArrayList<UsersInfo>();
		if(null == queryPage){
			objList = this.findByHQL(hql,null);
		} else {
			objList = this.findByHQL(queryPage,hql,null);
		}
		return objList;
	}

	@Override
	public UsersInfo getUserByNameLower(String userName) {
		HQLHelper hqlHelper = new HQLHelper("UsersInfo","user");
		String hql = hqlHelper.getHQL();
		hql = hql + " where Lower(user.userName) = :userName ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", userName);
		List<UsersInfo> objList = this.findByHQL(hql,params);
		if(null == objList || objList.size()==0)
			return null;
		return (UsersInfo)objList.get(0);
	}

	@Override
	public Integer getRefereeCount(String roleName) {
		String sql ="SELECT COUNT(u.`user_id`) FROM `m_system_role` r JOIN `m_system_role_user` ru ON r.id = ru.`ROLE_ID` JOIN `m_system_users` u ON ru.`USER_ID` = u.`user_id` WHERE r.`role_name` = '"+roleName+"'";
		List<Object[]> object = super.findBySQL(sql);
		if(null != object && object.size() > 0){
			return Integer.parseInt(String.valueOf(object.get(0)));
		}
		return 0;
	}

	@Override
	public List<UsersInfo> getNotRoleUserList(Long roleId, UsersBizBean vo, QueryPage queryPage) {
		StringBuffer sql = new StringBuffer("SELECT u.user_id,u.USER_NAME,u.password,u.realname,u.status FROM m_system_users u WHERE u.MARK_DELETE = 'N' ");
		if(null != vo){
			if(!StringUtils.isEmpty(vo.getUserName())){
				sql.append(" and u.USER_NAME like %:userName% ");
			}
			if(!StringUtils.isEmpty(vo.getRealName())){
				sql.append(" and u.realname like %:realName% ");
			}
		}
		sql.append(" AND u.user_id NOT IN(SELECT r.USER_ID FROM m_system_role_user r WHERE r.ROLE_ID = :roleId) order by u.user_id asc");
		List<UsersInfo> usersInfoList =  null;
		
		if(null != roleId){
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("roleId", roleId);
			if(null != vo && !StringUtils.isEmpty(vo.getUserName())){
				params.put("userName", vo.getUserName());
			}
			if(null != vo && !StringUtils.isEmpty(vo.getRealName())){
				params.put("realName", vo.getRealName());
			}
			
			List<Object[]> list=new ArrayList<Object[]>();
			
			if (queryPage == null) {
				list =(List<Object[]>)super.findBySQL(sql.toString(),params);
			} else {
				list =(List<Object[]>)super.findBySQL(queryPage, sql.toString(),params);
			}
			
			usersInfoList = new ArrayList<UsersInfo>();
			for(Object[] object:list){
				UsersInfo po = new UsersInfo();
				po.setUserId(object[0] != null ? Long.parseLong(object[0].toString()):null);
				po.setUserName(object[1] != null ? object[1].toString():null);
				po.setPassWord(object[2] != null ? object[2].toString():null);
				po.setRealName(object[3] != null ? object[3].toString():null);
				po.setStatus(object[4] != null ? object[4].toString():null);
				usersInfoList.add(po);
			}
		}
		
		return usersInfoList;
	}

	@Override
	public List<String> getRefereeName(String roleName) {
		String sql ="SELECT u.`realname` FROM `m_system_role` r JOIN `m_system_role_user` ru ON r.id = ru.`ROLE_ID` JOIN `m_system_users` u ON ru.`USER_ID` = u.`user_id` WHERE r.`role_name` = '"+roleName+"'";
		List<String> strs = new ArrayList<String>();
		List<Object[]> object = super.findBySQL(sql);
		if(null !=object && object.size() > 0){
			for (Object object2 : object) {
				strs.add(String.valueOf(object2));
			}
		}
		return strs;
	}
	
}
